<?php

class Antz_Pagination
{
    /**
     *  Makes a series of links to navigate multiple pages
     */         
    public function makeLinks($numPages, $pageNumber, $url, $lastPage = '<< ', $nextPage=' >>', $class='pagination',  $numLinks=4)
    {
        if($numPages == 0)$numPages = 1;
        if($pageNumber > $numPages)$pageNumber = $numPages;
        if($pageNumber < 1)$pageNumber = 1;
        $links = array();
        
        // make the back links
        if($pageNumber != 1){
            $links[] = '<a href="'.str_replace(':|pagenum|:', $pageNumber-1, $url).'" class="back">'.$lastPage.'</a>';
        };
        
        $linkStart = ceil($pageNumber - $numLinks/2);
        
        if($linkStart < 1){$linkStart = 1;};
        $linkEnd = $linkStart + $numLinks;
        if($linkEnd > $numPages){$linkEnd = $numPages;};
        // make sure it has the max num links
        if(($linkEnd - $numLinks) >= 1){$linkStart = $linkEnd-$numLinks;};
        
        for($i=$linkStart; $i<=$linkEnd; $i++){
            if($i == $pageNumber){
                // current page
                $links[] = '<span class="current">'.$i.'</span>';
            }else{
                $links[] = '<a href="'.str_replace(':|pagenum|:', $i, $url).'">'.$i.'</a>';
            };
        };
        
        // make the forward links
        if($pageNumber != $numPages){
            $links[] = '<a href="'.str_replace(':|pagenum|:', $pageNumber+1, $url).'" class="forward">'.$nextPage.'</a>';
        };
        
        return '<div class="'.$class.'">Page: '.implode('<span class="spacer">&nbsp;</span>', $links).'</div>';
        
    }
}
